﻿@typeparam TItem

<div class="selection-container">
    <CascadingValue Value="this">
        @ChildContent
    </CascadingValue>
</div>

@code {
    int _pageSize = 5;

    /// <summary>
    /// Page size.
    /// </summary>
    public int PageSize
    {
        get
        {
            return _pageSize;
        }
        set
        {
            if (_pageSize < 5)
            {
                _pageSize = 5;
            }
            _pageSize = value;
        }
    }

    /// <summary>
    /// How many page links there are before and after the selected page.
    /// </summary>
    [Parameter]
    public int Spread { get; set; } = 2;

    /// <summary>
    /// List where item is dragged.
    /// </summary>
    internal SelectionList<TItem>? DragSource;

    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// A new item has added for the list.
    /// </summary>
    [Parameter]
    public EventCallback<IEnumerable<TItem>> OnAdded { get; set; }

    /// <summary>
    /// Item has removed from the lists.
    /// </summary>
    [Parameter]
    public EventCallback<IEnumerable<TItem>> OnRemoved { get; set; }

    public List<TItem>? SourceList { get; set; }

    public TItem? Item { get; set; }

    internal async Task ItemAdded(SelectionList<TItem> target, IEnumerable<TItem> list)
    {
        await OnAdded.InvokeAsync(list);
        StateHasChanged();
    }

    internal async Task ItemRemoved(SelectionList<TItem> target, IEnumerable<TItem> list)
    {
        await OnRemoved.InvokeAsync(list);
        StateHasChanged();
    }
}
